<template>
  <div>
    <div style="height: 280px;overflow-y: auto;overflow-x: hidden">
      <el-tree
        :data="orgTree"
        :load="loadData"
        lazy
        show-checkbox
        check-strictly
        ref="orgTree"
        icon-class="ivu-icon-ios-arrow-forward"
        :props="orgTreeProps"
        node-key="id"
        @check-change="onTreeChange"
      ></el-tree>
    </div>
  </div>
</template>
<script>
import { getOrgsByPostId, findTreeList, findTreeNode } from "@/api/treeApi";
import { CoreModuleName } from "@/const.js";

export default {
  components: {},
  props: {
    rootOrgId: {
      type: String,
      default() {
        return null;
      }
    },
    rootOrgName: {
      type: String,
      default() {
        return null;
      }
    },
    orgId: {
      type: String,
      default() {
        return "";
      }
    },
    orgName: {
      type: String,
      default() {
        return "";
      }
    }
  },
  data() {
    return {
      orgTree: [],
      orgTreeProps: {
        children: "children",
        label: "nodeName"
      }
    };
  },
  computed: {},
  created: function() {
    this.$emit("SetTitle", "组织单选");
    this.$emit("SetPageWidth", 350);
    this.$emit("SetPageActions", [
      {
        text: "确定",
        theme: "primary",
        handle: () => {
          this.onOk();
        }
      },
      {
        text: "清空",
        theme: "primary",
        handle: () => {
          this.clear();
        }
      },
      {
        text: "关闭",
        theme: "primary",
        handle: () => {
          this.$emit("Close");
        }
      }
    ]);
  },
  beforeMount: function() {},
  mounted: function() {
    this.$refs["orgTree"].setCheckedKeys([this.orgId]);
  },
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    // 加载当前组织子节点
    loadData(currentOrg, callback) {
      // 查询通用树
      if (currentOrg.key) {
        findTreeNode(currentOrg.key).then(response => {
          let childrenOrg = response.data;
          callback(childrenOrg);
        });
      } else if (this.rootOrgId) {
        // findTreeNode(this.rootOrgId).then(response => {
        //   let childrenOrg = response.data;
        //   callback(childrenOrg);
        // });
        callback([
          {
            id: this.rootOrgId,
            nodeName: this.rootOrgName
          }
        ]);
      } else {
        const { postId } = $store.state[CoreModuleName].currentPost;

        getOrgsByPostId(postId).then(response => {
          let childrenOrg = response.data || [];
          if(childrenOrg.length === 0) {
            const nodeCode = window.RPConfig.node_code;
            findTreeList(nodeCode).then(response => {
              childrenOrg = response.data || [];
              callback(childrenOrg);
            });
            return
          }
          callback(childrenOrg);

        })
        
      }
    },
    onTreeChange(currentOrg) {
      if (this.$refs["orgTree"].getCheckedKeys() && this.$refs["orgTree"].getCheckedKeys().length == 2) {
        this.$refs["orgTree"].setCheckedKeys([currentOrg.id]);
      }
    },
    // 确定
    onOk() {
      let orgs = this.$refs["orgTree"].getCheckedNodes();
      let org = {};
      if (orgs && orgs.length > 0) {
        org = { orgId: orgs[0].id, orgName: orgs[0].nodeName };
      }
      this.$emit("Close", org);
    },
    // 清空
    clear() {
      this.$emit("Close", {});
    }
  },
  watch: {},
  directives: {}
};

</script>
